{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    85\n",
       "1    63\n",
       "2    51\n",
       "3    26\n",
       "4    30\n",
       "5     4\n",
       "6     7\n",
       "7     1\n",
       "8    17\n",
       "9    81\n",
       "dtype: int64"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame\n",
    "\n",
    "g = np.random.default_rng(0)\n",
    "s = Series(g.integers(0, 100, 10))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    8\n",
       "1    6\n",
       "2    5\n",
       "3    2\n",
       "4    3\n",
       "5    0\n",
       "6    0\n",
       "7    0\n",
       "8    1\n",
       "9    8\n",
       "dtype: int8"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# solution 1, using /\n",
    "(s / 10).astype(np.int8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    8\n",
       "1    6\n",
       "2    5\n",
       "3    2\n",
       "4    3\n",
       "5    0\n",
       "6    0\n",
       "7    0\n",
       "8    1\n",
       "9    8\n",
       "dtype: int64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# solution 2, using //\n",
    "(s // 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    8\n",
       "1    6\n",
       "2    5\n",
       "3    2\n",
       "4    3\n",
       "5    0\n",
       "6    0\n",
       "7    0\n",
       "8    1\n",
       "9    8\n",
       "dtype: object"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# solution 3, partial\n",
    "s.astype(str).str.get(-2).fillna('0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    8\n",
       "1    6\n",
       "2    5\n",
       "3    2\n",
       "4    3\n",
       "5    0\n",
       "6    0\n",
       "7    0\n",
       "8    1\n",
       "9    8\n",
       "dtype: int8"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# solution 3, complete\n",
    "s.astype(str).str.get(-2).fillna('0').astype(np.int8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    8\n",
       "1    6\n",
       "2    5\n",
       "3    2\n",
       "4    3\n",
       "5    0\n",
       "6    0\n",
       "7    0\n",
       "8    1\n",
       "9    8\n",
       "dtype: int8"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    s\n",
    "    .astype(str)     # get a series based on s, with dtype str\n",
    "    .str.get(-2)     # retrieve the second-to-last character\n",
    "    .fillna('0')     # replace NaN with '0'\n",
    "    .astype(np.int8) # get a new series back dtype int8\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
